home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / hity wydania / Ubuntu 9.10 PL / karmelkowy-koliberek-9.10-netbook-remix-PL.iso / casper / filesystem.squashfs / usr / share / pyshared / nevow / useragent.pyc (.txt) < prev    next >
Python Compiled Bytecode  |  2009-03-23  |  5KB  |  157 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. '''
  5. Parsers for browser User-Agent strings.
  6.  
  7. http://en.wikipedia.org/wiki/User_agent
  8. http://www.user-agents.org/
  9. '''
  10.  
  11. class browsers(object):
  12.     '''
  13.     Namespace class for Browser identifiers.
  14.     '''
  15.     GECKO = u'gecko'
  16.     INTERNET_EXPLORER = u'internet explorer'
  17.     WEBKIT = u'webkit'
  18.     OPERA = u'opera'
  19.  
  20.  
  21. class UserAgent(object):
  22.     '''
  23.     Structured representation of a version identifier of a web browser.
  24.  
  25.     This presents only minimal structured information about the agent
  26.     currently.  It could be expanded to include much more information, such a
  27.     security properties, platform, and native language.
  28.  
  29.     @type browser: C{unicode}
  30.     @ivar browser: The broad category of the browser.  Can only take on values
  31.         from L{browsers}.
  32.  
  33.     @type version: C{str}
  34.     @ivar version: The version claimed by the browser.
  35.     '''
  36.     
  37.     def __init__(self, browser, version):
  38.         '''
  39.         Initialize a new UserAgent.
  40.  
  41.         The positions of the arguments to this initializer are not stable.
  42.         Only pass arguments by keyword.
  43.         '''
  44.         self.browser = browser
  45.         self.version = version
  46.  
  47.     
  48.     def parse_GECKO(cls, agentString):
  49.         """
  50.         Attempt to parse the given User-Agent string as a Gecko-based browser's
  51.         user-agent.
  52.         """
  53.         identifier = 'Gecko/'
  54.         start = agentString.find(identifier)
  55.         if start != -1:
  56.             end = agentString.find(' ', start)
  57.             if end == -1:
  58.                 end = None
  59.             
  60.             version = agentString[start + len(identifier):end]
  61.             
  62.             try:
  63.                 version = int(version)
  64.             except ValueError:
  65.                 pass
  66.  
  67.             return cls(browsers.GECKO, (version,))
  68.         start != -1
  69.  
  70.     parse_GECKO = classmethod(parse_GECKO)
  71.     
  72.     def parse_WEBKIT(cls, agentString):
  73.         """
  74.         Attempt to parse the given User-Agent string as a WebKit-based
  75.         browser's user-agent.
  76.         """
  77.         identifier = 'WebKit/'
  78.         start = agentString.find(identifier)
  79.         if start != -1:
  80.             end = start + len(identifier)
  81.             while end < len(agentString) or agentString[end].isdigit() or agentString[end] == '.':
  82.                 end += 1
  83.             version = agentString[start + len(identifier):end]
  84.             
  85.             try:
  86.                 version = map(int, version.split('.'))
  87.             except ValueError:
  88.                 pass
  89.  
  90.             return cls(browsers.WEBKIT, tuple(version))
  91.         start != -1
  92.  
  93.     parse_WEBKIT = classmethod(parse_WEBKIT)
  94.     
  95.     def parse_OPERA(cls, agentString):
  96.         '''
  97.         Attempt to parse an Opera user-agent.
  98.         '''
  99.         prefix = 'Opera/'
  100.         if agentString.startswith(prefix):
  101.             version = agentString[len(prefix):].split(None, 1)[0]
  102.             
  103.             try:
  104.                 version = map(int, version.split('.'))
  105.             except ValueError:
  106.                 pass
  107.  
  108.             return cls(browsers.OPERA, tuple(version))
  109.         agentString.startswith(prefix)
  110.  
  111.     parse_OPERA = classmethod(parse_OPERA)
  112.     
  113.     def parse_MSIE(cls, agentString):
  114.         '''
  115.         Attempt to parse an Internet Explorer user-agent.
  116.         '''
  117.         oldPrefix = 'Mozilla/4.0 (compatible; MSIE '
  118.         newPrefix = 'Mozilla/5.0 (compatible; MSIE '
  119.         for prefix in (oldPrefix, newPrefix):
  120.             if agentString.startswith(prefix):
  121.                 end = agentString.find(';', len(prefix))
  122.                 if end == -1:
  123.                     end = None
  124.                 
  125.                 version = agentString[len(prefix):end]
  126.                 
  127.                 try:
  128.                     version = map(int, version.split('.'))
  129.                 except ValueError:
  130.                     pass
  131.  
  132.                 return cls(browsers.INTERNET_EXPLORER, tuple(version))
  133.             agentString.startswith(prefix)
  134.         
  135.  
  136.     parse_MSIE = classmethod(parse_MSIE)
  137.     
  138.     def fromHeaderValue(cls, agentString):
  139.         '''
  140.         Attempt to parse an arbitrary user-agent.
  141.  
  142.         @rtype: C{cls} or C{NoneType}
  143.         @return: A user agent object, or C{None} if parsing fails.
  144.         '''
  145.         for parser in [
  146.             'GECKO',
  147.             'WEBKIT',
  148.             'MSIE',
  149.             'OPERA']:
  150.             agent = getattr(cls, 'parse_' + parser)(agentString)
  151.             if agent is not None:
  152.                 return agent
  153.         
  154.  
  155.     fromHeaderValue = classmethod(fromHeaderValue)
  156.  
  157.